package org.jiaxin.nutrient.test;


import org.apache.commons.math3.optim.PointValuePair;
import org.apache.commons.math3.optim.linear.*;
import org.apache.commons.math3.optim.nonlinear.scalar.GoalType;

import java.util.Arrays;

/**
 * @author jiaxin
 * @version 1.0
 * @description: TODO
 * @date 2024/5/8 21:13
 */
public class ProductionScheduling {
    public static void main(String[] args) {
        LinearConstraint linearConstraint1 = new LinearConstraint(new double[]{1, -2, 1}, Relationship.LEQ, 11);
        LinearConstraint linearConstraint2 = new LinearConstraint(new double[]{-4, 1, 2}, Relationship.GEQ, 3);
        LinearConstraint linearConstraint3 = new LinearConstraint(new double[]{-2, 0, 1}, Relationship.EQ, 1);
        LinearConstraint linearConstraint4 = new LinearConstraint(new double[]{1, 0, 0}, Relationship.GEQ, 0);
        LinearConstraint linearConstraint5 = new LinearConstraint(new double[]{0, 1, 0}, Relationship.GEQ, 0);
        LinearConstraint linearConstraint6 = new LinearConstraint(new double[]{0, 0, 1}, Relationship.GEQ, 0);

        //将所有约束条件封装成一个约束集合
        LinearConstraintSet linearConstraintSet = new LinearConstraintSet(linearConstraint1, linearConstraint2,
                linearConstraint3, linearConstraint4, linearConstraint5, linearConstraint6);


        //定义约束函数，构造函数中第一个参数为约束函数系数， 第二个参数为约束函数常数项。
        LinearObjectiveFunction linearObjectiveFunction = new LinearObjectiveFunction(new double[]{-3, 1,1}, 0);

        //定义单纯型类
        SimplexSolver simplexSolver = new SimplexSolver();
        //传入参数进行优化，参数依次为，目标函数，约束条件，优化目标(求最大值，最小值)
        PointValuePair optimize = simplexSolver.optimize(linearObjectiveFunction, linearConstraintSet, GoalType.MINIMIZE);
        //打印结果
        System.out.println("决策变量为："+ Arrays.toString(optimize.getPoint()));
        System.out.println("最优值为："+optimize.getValue());
    }


}
